<template>
      <div class='readCardjy'>
        <el-button type="primary" @click="btnClicks">读取</el-button>
       
    </div>
</template>
<script>
import axios from 'axios'
export default {
    props:['id'],
    name:'read-cardlt',
    data(){
        return{
           isClick:true
        }
    },
    created(){
       
    },
    methods:{
        btnClicks(){
            if(this.isClick)
                this.readCard();
            else{
                this.$message.error('上一次读取还未结束,请不要重复读取');
            }
        },
       
        readCard(){
            this.isClick=false;
            let that=this;
            /*axios.post("http://127.0.0.1:38088/card=idcard").then((res)=>{
            });*/
            var ajax = new XMLHttpRequest();
            //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
            ajax.open('get','http://localhost:38088/card=idcard');
            //步骤三:发送请求
            ajax.send();
            //步骤四:注册事件 onreadystatechange 状态改变就会调用
            ajax.onreadystatechange = function () {
                that.isClick=true;
            if (ajax.readyState==4 &&ajax.status==200) {
                //步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的
                    let data=JSON.parse(ajax.responseText);
                    if(data.code=="0"){
                        let resultContent=data.IDCardInfo;
                        let partyName=resultContent.name;
                        let gender=resultContent.sexCode;
                        let nation=resultContent.nationCode;
                        let bornDay=resultContent.birthday;
                        let certAddress=resultContent.address;
                        let certNumber=resultContent.cardID;
                        let identityPic=resultContent.photoBase64;

                        let begin_data="data:image/jpeg;base64,";
                        let datas={};
                        datas.Born=bornDay.substring(0,4)+"-"+bornDay.substring(4,6)+"-"+bornDay.substring(6);
                        datas.labelA=partyName;
                        if(gender=="0"||gender=="1")
                            datas.Sex=gender=="0"?"女":"男";
                        else{
                            datas.Sex="";
                        }
                        datas.mz=nation+"族";
                        datas.Address=certAddress;
                        datas.CardNo=certNumber;
                        datas.image=begin_data+identityPic;
                        var date = new Date();
                        let curYear=date .getFullYear(); //获取完整的年份(4位)
                        let age=Number(curYear)-Number(bornDay.substring(0,4));
                        age=age.toFixed(0);
                        datas.age=age;
                        let ids=that.id.split("|");
                        for(let i=0;i<ids.length;i++){
                            let id_=ids[i].split(":");
                            if(id_[0]!="image"){
                                let data={};
                                let val=datas[id_[0]];
                                if(val){
                                    data.id=id_[1];
                                    data.val=val;
                                    
                                    that.$emit("input",JSON.stringify(data));
                                }
                                
                            }else{
                                that.$post("business","/api/file/fileUploadBase64",{data:identityPic,model:"头像",type:"头像",name:"头像"}).then(data=>{
                               
                                    let url=data[0].url;
                                    console.log("uploadImg==========================");
                                    console.log(url);
                                    let data_={};
                                    data_.id=id_[1];
                                    data_.val=url;
                                    that.$emit("input",JSON.stringify(data_));
                                });
                            }


                            
                        }
                    }
            　　}
            }

            
        }
      
        
    }
}
</script>